package com.masterviame.Authc.service.Impl;

import com.masterviame.Authc.mapper.PermissionMapper;
import com.masterviame.Authc.mapper.RoleMapper;
import com.masterviame.Authc.mapper.UserMapper;
import com.masterviame.Authc.pojo.Permission;
import com.masterviame.Authc.pojo.Role;
import com.masterviame.Authc.pojo.User;
import com.masterviame.Authc.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by mastervictory on 2017/12/13.
 */
@Service
@Transactional(propagation = Propagation.REQUIRED,
isolation = Isolation.DEFAULT,timeout = 20)
public class UserServiceImpl implements UserService{
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public void createUser(User user) {
        if (user==null)
            throw new NullPointerException("value is null");
        userMapper.addUser(user);
    }

    @Override
    public void changePassword(long userid, String Password) {
        User oldUser=userMapper.getUserById(userid);
        if (oldUser==null)
            throw new NullPointerException("oldUser is not exist");
        oldUser.setPassword(Password);
        userMapper.updateUser(oldUser);
    }

    @Override
    public void correlationRoles(long userid, long... roleids) {
        String RoleCode="";
        for (long id:roleids)
         userMapper.addUserRole(userid,id);
    }

    @Override
    public User findByUsername(String username) {
        if (StringUtils.isBlank(username))
            throw new NullPointerException("value is not exist");
        User user=userMapper.getUserByuserName(username);
        if (user==null)
            throw new NullPointerException("Object is NULL");
        return user;
    }

    @Override
    public Set<String> findRoles(String username) {
          Set<String> roles=new HashSet<>();
          User user=findByUsername(username);
          String[] ids=user.getRoleCode().split(",");
          for (String id:ids){
              Role role=roleMapper.getRolebyid(Long.parseLong(id));
              if (role.isAvailable())
              roles.add(role.getRole());
          }
          if (roles.size()>0)
              return roles;
        else
            throw new NullPointerException("该用户没有角色");
    }

    @Override
    public Set<String> findPermissions(String username) {
        Set<String> roles=new HashSet<>();
        Set<String> permissions=new HashSet<>();
        User user=findByUsername(username);
        String[] ids=user.getRoleCode().split(",");
        for (String id:ids){
            Role role=roleMapper.getRolebyid(Long.parseLong(id));
            if (role.isAvailable()){
                String[] pids=role.getPermissionsCode().split(",");
                for (String pid:pids){
                    Permission permission=permissionMapper.
                            getPermissionByid(Long.parseLong(id));
                    if (permission.isAvailable())
                    permissions.add(permission.getPermission());
                }
            }
        }
        if (permissions.size()>0)
            return permissions;
        else
            throw new NullPointerException("permission or role is no exist");
    }

    @Override
    public List<User> getallUsers() {
        List<User> users=userMapper.getUsers();
        if (users==null)
            throw new NullPointerException("user is not exist");
        return users;
    }

    @Override
    public void updateUser(User user) {
        if (user!=null){
            userMapper.updateUser(user);
        }
    }

    @Override
    public void deleteUser(long userid) {
        userMapper.deleteUserById(userid);
    }

    @Override
    public User getUser(long userid) {
        User user=userMapper.getUserById(userid);
        if (user!=null)
            return user;
        throw new NullPointerException("get value is null");
    }

    @Override
    public void deleteUserRole(long userid, long... roleid) {
        User user=userMapper.getUserById(userid);
        String roleCode=user.getRoleCode();
        for (long id:roleid){
            roleCode.replace(id+",","");
        }
        user.setRoleCode(roleCode);
        userMapper.updateUser(user);
    }

    @Override
    public void deleteUserRoles(long userid) {
       User user=userMapper.getUserById(userid);
       user.setRoleCode("0");
       userMapper.updateUser(user);
    }

    @Override
    public void lock(long userid) {
        User user=userMapper.getUserById(userid);
        if (user!=null){
            user.setLocked(true);
            userMapper.updateUser(user);
        }
    }
}
